package com.base.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommExcel;
import com.base.po.sqlparam.ShopAliOrderExamplePage;
import com.base.po.voip.ShopAliOrder;
import com.base.po.voip.ShopAliOrderExample.Criteria;
import com.base.po.voip.ShopOrder;
import com.base.po.voip.Subscribers;
import com.base.po.voip.SubscribersExample;
import com.base.po.voip.Users;
import com.base.service.ShopAliOrderService;
import com.base.service.ShopOrderService;
import com.base.service.UsersService;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_taobaodingdan {
	@Autowired
	private ShopAliOrderService shopAliOrderService;
	@Autowired
	private ShopOrderService shopOrderService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private SubscribersService subscribersService;

	// 商品信息
	@RequestMapping("/content_taobaodingdan")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		// updateReappearing(adminUserId,adminUsergrade);
		ShopAliOrderExamplePage examplePage = new ShopAliOrderExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime desc");
		List<ShopAliOrder> shopAliOrders = null;
		List<String> list = null;
		Criteria a = examplePage.createCriteria();

		switch (adminUsergrade) {
		case 0:
			a.andDatastatusEqualTo(true);
			break;

		case 1:
			// 查询OEMID下的用户
			// 查询OEM下的商户以及OEM
			// 查询OEM id
			list = usersService.selectByParentId(adminUserId);
			for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId));
			}
			examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
			break;

		case 2:
			list = usersService.selectByShopId(adminUserId);
			for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId));
			}
			examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
			break;
		}
		/*
		 * 设置分页
		 */
		int totlenum = shopAliOrderService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", 1);// 当前页面传入session中
		shopAliOrders = shopAliOrderService.selectByExamplePage(examplePage);
		// 用户名
		Subscribers subscribers = null;
		// 订单管理
		ShopOrder shopOrder = null;
		// 代理商名
		Users pUser = null;
		for (ShopAliOrder s : shopAliOrders) {

			shopOrder = shopOrderService.selectByOrdernum(s.getOrdernum());

			if (shopOrder != null) {
				subscribers = subscribersService.selectByPrimaryKey(shopOrder.getUserid());
			}
			// 存入用户手机号
			if (subscribers != null && subscribers.getMobile() != null) {
				s.setUserid(subscribers.getMobile());
			} else {
				s.setUserid("未知");
			}

			pUser = usersService.selectByPrimaryKey(s.getAgentid());
			// 存入代理商名称
			if (pUser != null && pUser.getUsername() != null) {
				s.setAgentid(pUser.getUsername());
			} else {
				s.setUserid("未知");
			}

			// 创建时间和当前日期相差天数
			int cDate = 0;
			// 当前日期实际返现金额
			BigDecimal b = new BigDecimal(0);

			if (s.getStatus() == 0) {
				s.setReappearing(new Float(0.00));
			}

			if (s.getStatus() == 1) {
				cDate = Produce.differentDays(s.getClicktime(), Produce.getNowDate());

				b = s.getJsmoney().multiply(new BigDecimal(cDate * 0.0005));
				// 等于
				if (b.compareTo(s.getJsmoney()) == 0) {
					// 等于的时候返现停止显示为结算的金额
					s.setReappearing((s.getJsmoney()).floatValue());
					// 等于的时候返现停止显示状态为2
					s.setStatus(2);
					// 数据库状态更新为 2
					shopAliOrderService.updateByOrderNum(2, s.getOrdernum());
				}
				// 小于
				if (b.compareTo(s.getJsmoney()) == -1) {
					s.setReappearing(b.floatValue());
				}
				// 大于
				if (b.compareTo(s.getJsmoney()) == 1) {
					// 大于的时候返现停止显示为结算的金额
					s.setReappearing((s.getJsmoney()).floatValue());
					// 大于的时候返现停止显示状态为2
					s.setStatus(2);
					// 数据库状态更新为 2
					shopAliOrderService.updateByOrderNum(2, s.getOrdernum());
				}

			}
		}

		CommExcel.list.clear();
		// 设置页面进行跳转
		request.setAttribute("shopAliOrders", shopAliOrders);
		modelAndView.setViewName("jsp/content_taobaodingdan");
		return modelAndView;
	}

	// 商品信息
	@RequestMapping("/selectShopAliOrder")
	public ModelAndView selectShopAliOrder(HttpServletRequest request, Integer curPage, String ordernum, String mobile,
			Integer status) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(curPage);
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		if (status == null) {
			if (request.getSession().getAttribute("status") != null) {
				status = (Integer) request.getSession().getAttribute("status");
			}
		}
		if (status == null) {
			if (request.getSession().getAttribute("status") != null) {
				status = (Integer) request.getSession().getAttribute("status");
			}
		}
		System.out.println(curPage);
		ShopAliOrderExamplePage examplePage = new ShopAliOrderExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime desc");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		// List<String> u =null;
		// if(adminUsergrade == 1) {
		// u =usersService.selectByParentId(adminUserId);
		// }
		// if(adminUsergrade == 2) {
		// u = usersService.selectByShopId(adminUserId);
		// }
		Integer choose = 2;
		if (ordernum != null && !ordernum.equals("") || status != null && !status.equals("")
				|| mobile != null && !mobile.equals("")) {
			choose = 1;
		}

		System.out.println(choose);
		System.out.println("++++++" + status + "++++++");
		// 分页集合
		List<ShopAliOrder> shopAliOrder = null;
		Criteria a = examplePage.createCriteria();

		if (adminUsergrade == 1) {
			List<String> list = null;
			examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true));

			if (mobile != null && !mobile.equals("")) {
				SubscribersExample subscribersExample = new SubscribersExample();
				SubscribersExample.Criteria b = subscribersExample.createCriteria();
				b.andMobileEqualTo(mobile).andDatastatusEqualTo(true).andParentidEqualTo(adminUserId);
				List<Subscribers> sList = subscribersService.selectByExample(subscribersExample);
				Subscribers sub = null;
				list = usersService.selectByParentId(adminUserId);
				if (sList != null && sList.size() > 0) {
					sub= sList.get(0);
				}
//				examplePage.createCriteria().andAgentidEqualTo(adminUserId).andUseridEqualTo(sub.getId());
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId)
							.andUseridEqualTo(sub.getId()));
				}
				examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId)
						.andUseridEqualTo(sub.getId()));
				status = null;
			}

			if (ordernum != null && !ordernum.equals("")) {
				list = usersService.selectByParentId(adminUserId);
				
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId)
							.andOrdernumEqualTo(ordernum));
				}
				examplePage.getOredCriteria()
						.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andOrdernumEqualTo(ordernum));
				status = null;
			}
			if (status != null) {
				// 判断当前可用性
				switch (status) {
				case 0:
					list = usersService.selectByParentId(adminUserId);
					for (String oemId : list) {
						// OEM下的商家
						examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId)
								.andStatusEqualTo(0));
					}
					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(0));
					break;
				case 1:
					list = usersService.selectByParentId(adminUserId);
					for (String oemId : list) {
						// OEM下的商家
						examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId)
								.andStatusEqualTo(1));
					}
					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(1));
					break;
				case 2:
					list = usersService.selectByParentId(adminUserId);
					for (String oemId : list) {
						// OEM下的商家
						examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId)
								.andStatusEqualTo(2));
					}
					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(2));
					break;
				case 3:
					list = usersService.selectByParentId(adminUserId);
					for (String oemId : list) {
						// OEM下的商家
						examplePage
								.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId));
					}
					examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
					break;
				}
			}
		}

		if (adminUsergrade == 2) {
			examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true));

			if (mobile != null && !mobile.equals("")) {
				SubscribersExample subscribersExample = new SubscribersExample();
				SubscribersExample.Criteria b = subscribersExample.createCriteria();
				b.andMobileEqualTo(mobile).andDatastatusEqualTo(true);
				List<Subscribers> sList = subscribersService.selectByExample(subscribersExample);
                Subscribers sub = null;
				if (sList != null && sList.size() > 0) {
					sub= sList.get(0);
				}
				examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId)
						.andUseridEqualTo(sub.getId()));
				status = null;
			}

			if (ordernum != null && !ordernum.equals("")) {

				examplePage.getOredCriteria()
						.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andOrdernumEqualTo(ordernum));
				status = null;
			}
			if (status != null) {
				// 判断当前可用性
				switch (status) {
				case 0:

					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(0));
					break;
				case 1:

					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(1));
					break;
				case 2:

					examplePage.getOredCriteria()
							.add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(2));
					break;
				case 3:
					examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
					break;
				}
			}
		}

		if (choose == 2) {
			List<String> list = null;
			switch (adminUsergrade) {
			case 0:
				a.andDatastatusEqualTo(true);
				break;

			case 1:
				// 查询OEMID下的用户
				// 查询OEM下的商户以及OEM
				// 查询OEM id
				list = usersService.selectByParentId(adminUserId);
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andDatastatusEqualTo(true).andAgentidEqualTo(oemId));
				}
				examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
				break;

			case 2:

				examplePage.getOredCriteria().add(a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId));
				break;
			}
		}
		/*
		 * 设置分页
		 */
		int totlenum = shopAliOrderService.countByExample(examplePage);// 页面总数

		// Page page = new Page(); // 实例化分页工具类
		// if(mobile != null && !mobile.equals("")) {
		// Users users = usersService.selectByPrimaryKey(adminUserId);
		//
		// if(users.getRole() == 1) {
		// if(subscribersService.selectByMobileAndParentId(mobile,adminUserId).size()>0)
		// {
		// page.setCurrentPageNo(curPage); // 当前页 默认1
		// String userid=
		// subscribersService.selectByMobileAndParentId(mobile,adminUserId).get(0);
		// System.out.println(userid);
		// page.setRecordTotal(shopAliOrderService.countByUserID(userid));
		// shopAliOrder = shopAliOrderService.selectByUserID(page,userid);
		// request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		// request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		// }else {
		// shopAliOrder = null;
		// request.setAttribute("totlenum", page.getRecordTotal());// 总数据传入session中
		// request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		// }
		// }
		// if(users.getRole() == 2) {
		// if(subscribersService.selectByMobileAndShopId(mobile,adminUserId).size()>0) {
		// page.setCurrentPageNo(curPage); // 当前页 默认1
		// String userid=
		// subscribersService.selectByMobileAndShopId(mobile,adminUserId).get(0);
		// System.out.println(userid);
		// page.setRecordTotal(shopAliOrderService.countByUserID(userid));
		// shopAliOrder = shopAliOrderService.selectByUserID(page,userid);
		// request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		// request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		// }else {
		// shopAliOrder = null;
		// request.setAttribute("totlenum", page.getRecordTotal());// 总数据传入session中
		// request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		// }
		// }
		//
		//
		// }else {
		shopAliOrder = shopAliOrderService.selectByExamplePage(examplePage);
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		// }

		// 用户名
		Subscribers subscribers = null;
		// 代理商名
		Users pUser = null;
		// 创建时间和当前日期相差天数
		int cDate = 0;
		// 到当前日期为止实际返现金额
		BigDecimal b = new BigDecimal(0);
		if (shopAliOrder != null && shopAliOrder.size() > 0) {
			for (ShopAliOrder s : shopAliOrder) {
				subscribers = subscribersService.selectByPrimaryKey(s.getUserid());

				if (subscribers != null && subscribers.getMobile() != null) {
					s.setUserid(subscribers.getMobile());
				} else {
					s.setUserid("未知");
				}

				pUser = usersService.selectByPrimaryKey(s.getAgentid());

				if (pUser != null && pUser.getUsername() != null) {
					s.setAgentid(pUser.getUsername());
				} else {
					s.setUserid("未知");
				}

				// 返现金额
				if (s.getStatus() == 1) {
					cDate = Produce.differentDays(s.getClicktime(), Produce.getNowDate());

					b = s.getJsmoney().multiply(new BigDecimal(cDate * 0.0005));
					// 等于
					if (b.compareTo(s.getJsmoney()) == 0) {
						// 等于的时候返现停止显示为结算的金额
						s.setReappearing((s.getJsmoney()).floatValue());
						// 等于的时候返现停止显示为状态为2
						s.setStatus(2);
						// 数据库状态更新为 2
						shopAliOrderService.updateByOrderNum(2, s.getOrdernum());
					}
					// 小于
					if (b.compareTo(s.getJsmoney()) == -1) {
						s.setReappearing(b.floatValue());
					}
					// 大于
					if (b.compareTo(s.getJsmoney()) == 1) {
						// 大于的时候返现停止显示为结算的金额
						s.setReappearing((s.getJsmoney()).floatValue());
						// 大于的时候返现停止显示为状态为2
						s.setStatus(2);
						// 数据库状态更新为 2
						shopAliOrderService.updateByOrderNum(2, s.getOrdernum());
					}

				}
			}
		}

		// 设置页面进行跳转
		request.setAttribute("shopAliOrders", shopAliOrder);
		request.setAttribute("ordernum", ordernum);
		request.setAttribute("mobile", mobile);
		request.setAttribute("status", status);
		modelAndView.setViewName("jsp/content_taobaodingdan");
		return modelAndView;
	}

	// //更新当日返现
	// public void updateReappearing(String adminUserId,Integer adminUsergrade) {
	// ShopAliOrderExample example = new ShopAliOrderExample();
	// List<ShopAliOrder> shopAliOrders = null;
	// Criteria a = example.createCriteria();
	//
	// //查找所有状态为1的订单
	// switch (adminUsergrade) {
	// case 0:
	// a.andDatastatusEqualTo(true).andStatusEqualTo(1);
	// break;
	//
	// case 1:
	// a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(1);
	// break;
	//
	// case 2:
	// // 查询商家下的ID
	// a.andDatastatusEqualTo(true).andAgentidEqualTo(adminUserId).andStatusEqualTo(1);
	// break;
	// }
	// shopAliOrders = shopAliOrderService.selectByExample(example);
	//
	// //创建时间和当前日期相差天数
	// int cDate = 0;
	// //当前日期实际返现金额
	// BigDecimal b = new BigDecimal(0);
	// for(ShopAliOrder s : shopAliOrders) {
	// if(s.getStatus() == 1) {
	// cDate=Produce.differentDays(s.getCreatetime(), Produce.getNowDate());
	//
	// b=s.getJsmoney().multiply(new BigDecimal(cDate*0.005));
	// //等于
	// if(b.compareTo(s.getJsmoney()) == 0) {
	// ShopAliOrder s1 = new
	// ShopAliOrder(s.getId(),s.getCreatetime(),s.getClicktime(),
	// s.getTitle(),s.getCid(),s.getMoney(),s.getOutmoney(),s.getJsmoney(),
	// s.getYjbl(),s.getYjmoney(),s.getOrdernum(),s.getAgentid(),
	// s.getDatastatus(),2,b.floatValue(),s.getNotreappearing(),s.getUserid());
	// shopAliOrderService.updateByPrimaryKey(s1);
	// }
	// //小于
	// if(b.compareTo(s.getJsmoney()) == -1) {
	// ShopAliOrder s2 = new
	// ShopAliOrder(s.getId(),s.getCreatetime(),s.getClicktime(),
	// s.getTitle(),s.getCid(),s.getMoney(),s.getOutmoney(),s.getJsmoney(),
	// s.getYjbl(),s.getYjmoney(),s.getOrdernum(),s.getAgentid(),
	// s.getDatastatus(),1,b.floatValue(),s.getNotreappearing(),s.getUserid());
	// shopAliOrderService.updateByPrimaryKey(s2);
	// }
	// //大于
	// if(b.compareTo(s.getJsmoney()) == 1) {
	// ShopAliOrder s3 = new
	// ShopAliOrder(s.getId(),s.getCreatetime(),s.getClicktime(),
	// s.getTitle(),s.getCid(),s.getMoney(),s.getOutmoney(),s.getJsmoney(),
	// s.getYjbl(),s.getYjmoney(),s.getOrdernum(),s.getAgentid(),
	// s.getDatastatus(),2,s.getJsmoney().floatValue(),s.getNotreappearing(),s.getUserid());
	// shopAliOrderService.updateByPrimaryKey(s3);
	// }
	// }
	// }
	// }

}
